/**
 * Copyright (c) 2018 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package com.lswms.common.interceptor;

import com.baomidou.mybatisplus.core.toolkit.PluginUtils;
import com.baomidou.mybatisplus.extension.handlers.AbstractSqlParserHandler;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;

import java.sql.Connection;
import java.util.Map;
import java.util.Properties;

/**
 * 数据过滤
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0
 */
@Intercepts({
		@Signature(type = StatementHandler.class, method = "prepare", args = { Connection.class, Integer.class }) })
public class DataFilterInterceptor extends AbstractSqlParserHandler implements Interceptor {

	@Override
	public Object intercept(Invocation invocation) throws Throwable {
		StatementHandler statementHandler = (StatementHandler) PluginUtils.realTarget(invocation.getTarget());
		MetaObject metaObject = SystemMetaObject.forObject(statementHandler);

		// SQL解析
		this.sqlParser(metaObject);

		// 先判断是不是SELECT操作
		MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement");
		if (!SqlCommandType.SELECT.equals(mappedStatement.getSqlCommandType())) {
			return invocation.proceed();
		}

		// 针对定义了rowBounds，做为mapper接口方法的参数
		BoundSql boundSql = (BoundSql) metaObject.getValue("delegate.boundSql");
		String originalSql = boundSql.getSql();
		Object paramObj = boundSql.getParameterObject();

		// 判断参数里是否有DataScope对象
		DataScope scope = null;
		if (paramObj instanceof DataScope) {
			scope = (DataScope) paramObj;
		} else if (paramObj instanceof Map) {
			for (Object arg : ((Map) paramObj).values()) {
				if (arg instanceof DataScope) {
					scope = (DataScope) arg;
					break;
				}
			}
		}

		// 不用数据过滤
		if (scope == null) {
			return invocation.proceed();
		}

		// 拼接新SQL
		originalSql = originalSql + scope.getSqlFilter();

		// 重写SQL
		metaObject.setValue("delegate.boundSql.sql", originalSql);
		return invocation.proceed();
	}

	@Override
	public Object plugin(Object target) {
		if (target instanceof StatementHandler) {
			return Plugin.wrap(target, this);
		}
		return target;
	}

	@Override
	public void setProperties(Properties properties) {

	}
}